import Vue from 'vue'
import store from '@/store'


/**
 * 判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
function hasPermission(key) {
  let hasPermission = false
  if (key) {
    // 初始size
    let i = 0
    let keys = []
    keys = key.split(',')
    //return true;
    for (const permission of store.getters.permissions) {
      keys.forEach(item => {
        if (permission.menuRouterName === item) {
          i++
        }
      })
      if (i === keys.length) {
        break
      }
    }
    // 每个元素都有权限才为true
    if (i === keys.length) {
      hasPermission = true
    }
  }
  return hasPermission
}

const has = Vue.directive('has', {
  bind: function(el, binding) {
    // 判断按钮数组里是否存在该按钮，如没有则移除该按钮
    if (!hasPermission(binding.value)) {
      el.parentNode.removeChild(el)
    }
  }
})

Vue.prototype.hasPermission = hasPermission;
export default has

